/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package clueless;

import java.awt.Color;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.ArrayList;

/**
 *
 * @author dcain
 */
public class Clueless {

    /**
     * @param args the command line arguments
     */
    //static StartEndDialog startDialog = new StartEndDialog("Welcome to Clueless! Press Start to continue.", "Start"); 
    public static ClientUI UI;
    static boolean uiStarted = false;
    static String input = "Welcome to Clueless!";
    static String name;
    static ArrayList<String> hand = new ArrayList();
    static Player[] players;
    static String[] weaponName = {"Candlestick", "Knife", "Pipe", "Revolver", "Rope", "Wrench"};
    static String[] playerName = {"Scarlet","Plum","Mustard","Peacock", "Green","White"};
    static Color[] playerColors = {Color.RED, Color.DARK_GRAY, Color.YELLOW, Color.BLUE, Color.GREEN, Color.WHITE};
    static String[] roomName = {"Study", "Hall", "Lounge", "Library", "Billiard_Room", "Dining_Room", "Conservatory", "Ballroom", "Kitchen"};
    String[] hallList = {"ScarletStart","PlumStart","MustardStart","PeacockStart", "GreenStart","WhiteStart", "Study_Hall", "Hall_Lounge", "Study_Library", "Hall_Billiard", 
        "Lounge_Dining", "Library_Billiard", "Billiard_Dining", "Library_Conservatory", 
        "Billiard_Ballroom", "Dining_Kitchen", "Conservatory_Ballroom", "Ballroom_Kitchen"};
    static PrintWriter out;
    static BufferedReader in;
    static String output;
    static String inLine;
    static String room;

        
    
    public static void main(String[] args) throws IOException, InterruptedException {
        java.net.InetAddress localMachine = java.net.InetAddress.getLocalHost();
        String hostName = localMachine.getHostName();
        System.out.println("Hostname of local machine: " + localMachine.getHostName());
        StartEndDialog stDialog = new StartEndDialog();
        stDialog.setVisible(true);
        //ClientUI UI = new ClientUI();        
        Socket socket = new Socket(hostName, 4444);        
        out = new PrintWriter(socket.getOutputStream(), true);        
        in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        String fromServer = null;
        String chTaken = "";
        while((fromServer = in.readLine()) != null){
            System.out.println(fromServer);
            if(fromServer.startsWith("Prompt")){
                 if(fromServer.length()>6){
                     chTaken = fromServer.substring(7);
                     stDialog.setChars(chTaken);
                     stDialog.setTitle("Select a Character!");
                     
                 } else {
                    stDialog.setTitle("Select a Character!");
                 }
                 
             } else if(fromServer.startsWith("Message")){
                if(!uiStarted){
                    UI = new ClientUI();
                    UI.setVisible(true);
                    uiStarted = true;
                }
                UI.updateText(fromServer.substring(8));
                fromServer = null;
                
             } else if (fromServer.startsWith("Hand")){
                String procHand = fromServer.substring(5);                 
                UI.checkHand(procHand);
                fromServer = null;
                
             } else if (fromServer.startsWith("Moves")){                 
                System.out.println("From serv: "+fromServer);
                String procMoves[] = fromServer.split(" ");
                UI.processMoves(procMoves);
                fromServer = null;
                
             } else if (fromServer.startsWith("Refute")){
                 System.out.println("Clueless fromSever Refute: "+ fromServer);
                 String procRefute[] = fromServer.split(" ");
                 String playerWhoSugg = procRefute[1];
                 String suggestion[] = new String [3];
                 suggestion[0] = procRefute[2];
                 suggestion[1] = procRefute[3];
                 suggestion[2] = procRefute[4];
                 String refutable[] = new String [3];
                 refutable[0] = procRefute[5];
                 refutable[1] = procRefute[6];
                 refutable[2] = procRefute[7];
                 SuggAlertDialog refDialog = new SuggAlertDialog(playerWhoSugg, suggestion, refutable);
                 refDialog.setVisible(true);
             } else if (fromServer.startsWith("Update")){
                  System.out.println("fromSever Update: "+ fromServer);
                  String procUpdate[] = fromServer.split(" ");
                  processUpdate(procUpdate);
             } else if(fromServer.startsWith("GameOver")){
                 String[] gameOver = fromServer.split(" ");
                 GameOverDialog gameOverDialog = new GameOverDialog(gameOver[1]);
                 gameOverDialog.setVisible(true);
             }
        }             
    }

    static void sendMove(String dir) {
        out.println("Move " + dir);
        System.out.println("Sending Move " +dir);
    }
    
    static void sendSuggest(String[] sugg){
        out.println("Suggest " + sugg[1] + " " + sugg[0]+ " " + sugg[2]);        
    }

    static void sendAccuse(String[] sugg) {
        out.println("Accuse " + sugg[1] + " " + sugg[0]+ " " + sugg[2]);        
    }

    static void sendEndTurn() {
        System.out.println("EndTurn sent");
        out.println("EndTurn");
    }

    private static void processUpdate(String[] update) throws InterruptedException {
        UI.clearPositions();
        for(int i = 1; i < 13; i+=2){
            System.out.println(update[2] + " " + update[1]);
            UI.setPlayer(update[i+1],update[i]); 
       }
    }

    static void sendRestart() {
        UI.dispose();
        System.exit(0);
    }
}
